package dk.controlme.lego.client;

import java.io.DataInputStream;
import lejos.nxt.comm.BTConnection;

public class BluetoothConnection extends Thread {
    DataInputStream datainputstream;
    private boolean newCommand = true;
    private int command;

    public BluetoothConnection(BTConnection btConnection) {
        datainputstream = btConnection.openDataInputStream();
    }

    public void run() {
        command = -1;
        while(true) {
            try{
                newCommand = false;
                command = datainputstream.readInt();
                newCommand = true;
                try{
                    Thread.sleep(200);
                } catch (Exception e) {
                    // Do nothing (failure to sleep is OK)
                }
            } catch (Exception e) {
                // Do nothing (failed to open I/O stream)
            }
        }
    }

    /**
     * Numeric (int) value of the command we were sent
     * @return the current command sent from bluetooth
     */
    public int getCommand() {
        return command;
    }

    /**
     * If we have a new command waiting.
     * @return true if we have received a new command via BT
     */
    public boolean getNewCommand() {
        return newCommand;
    }
}
